﻿using ZhouYu.DreamTour.Web.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZhouYu.DreamTour.Core;

using System.Text;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using ViewResult = Microsoft.AspNetCore.Mvc.ViewResult;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using ContentResult = Microsoft.AspNetCore.Mvc.ContentResult;

namespace ZhouYu.DreamTour.Attributes
{

    public class ActionAttribute:ActionFilterAttribute  //行为过滤器
    {

        /// <summary>
        /// 调用控制器对应的Action方法之后的操作
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            ////获取结果 
            //var actionResult = context.Result;
            ////判断结果是否是一个ViewResult 
            //if (actionResult is ViewResult)
            //{
            //    var viewResult = actionResult as ViewResult;
            //    //下面的代码就是执行这个 ViewResult，并把结果的html内容放到一个StringBuiler对象中 
            //    var services = context.HttpContext.RequestServices;
            //    var executor = services.GetRequiredService<IActionResultExecutor<ViewResult>>() as ViewResultExecutor;
            //    var option = services.GetRequiredService<IOptions<MvcViewOptions>>();
            //    var result = executor.FindView(context, viewResult);
            //    result.EnsureSuccessful(originalLocations: null);
            //    var view = result.View;
            //    var builder = new StringBuilder();
            //    using (var writer = new StringWriter(builder))
            //    {
            //        var viewContext = new ViewContext(
            //            context,
            //            view,
            //            viewResult.ViewData,
            //            viewResult.TempData,
            //            writer,                
            //            option.Value.HtmlHelperOptions);
            //        view.RenderAsync(viewContext).GetAwaiter().GetResult();
            //        //这句一定要调用，否则内容就会是空的 
            //        writer.Flush();
            //    }
            //    //按照规则生成静态文件名称 
            //    var year = context.RouteData.Values["year"].ToString();
            //    var month = context.RouteData.Values["month"].ToString();
            //    var slug = context.RouteData.Values["slug"].ToString();
            //    var fileDir = BlogRoutes.GetPostFileCacheDirectory(year, month);
            //    if (!Directory.Exists(fileDir))
            //    {
            //        Directory.CreateDirectory(fileDir);
            //    }
            //    var filePath = BlogRoutes.GetPostFileCachePath(year, month, slug);
            //    using (FileStream fs = File.Open(filePath, FileMode.Create))
            //    {
            //        using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
            //        {
            //            sw.Write(builder.ToString());
            //        }
            //    }
            //    //输出当前的结果 
            //    var contentresult = new ContentResult();
            //    contentresult.Content = builder.ToString();
            //    contentresult.ContentType = "text/html";
            //    context.Result = contentresult;
            //}





            //string controller = filterContext.RouteData.Values["Controller"].ToString();
            //string action = filterContext.RouteData.Values["Action"].ToString();
            //if (action == "LoginOut")
            //{
            //    return;
            //}
            //if (action == "Login")
            //{
            //    //获取Ip
            //    string ip = LocalIP.GetLocalIP();
            //    //获取系统的日志
            //    string msg = $"时间:{DateTime.Now},Ip:{ip},已完成控制器:{controller},页面:{action}\r\n";
            //    //获取文件路径
            //    string path = "D:\\Action.txt";
            //    File.AppendAllText(path, msg);
            //}
            //else
            //{
            //    //获取用户名
            //    string username = UserInfoContext.userInfoContext.UserInfo.UserName;
            //    //获取系统的日志
            //    string msg = $"时间:{DateTime.Now},用户名:{username},已完成控制器:{controller},页面:{action}\r\n";
            //    //获取文件路径
            //    string path = "D:\\Action.txt";
            //    File.AppendAllText(path, msg);
            //}
        }


        /// <summary>
        /// 调用控制器对应的Action方法之前的操作
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {

            //var year = context.RouteData.Values["year"].ToString();
            //var month = context.RouteData.Values["month"].ToString();
            //var slug = context.RouteData.Values["slug"].ToString();
            //var filePath = BlogRoutes.GetPostFileCachePath(year, month, slug);
            ////判断文件是否存在 
            //if (File.Exists(filePath))
            //{
            //    //获取文件信息对象 
            //    var fileInfo = new FileInfo(filePath);
            //    //结算时间间隔，如果小于等于15分钟，就直接输出 
            //    TimeSpan ts = DateTime.Now - fileInfo.LastWriteTime;
            //    if (ts.TotalMinutes <= 15)
            //    {
            //        //如果存在，直接读取文件 
            //        using (var fs = File.Open(filePath, FileMode.Open))
            //        {
            //            using (var sr = new StreamReader(fs, Encoding.UTF8))
            //            {
            //                //通过contentresult返回文件内容 
            //                var contentresult = new ContentResult();
            //                contentresult.Content = sr.ReadToEnd();
            //                contentresult.ContentType = "text/html";
            //                context.Result = contentresult;
            //            }
            //        }
            //    }
            //}




            //string controller = filterContext.RouteData.Values["Controller"].ToString();
            //string action = filterContext.RouteData.Values["Action"].ToString();
            //if (action == "Login")
            //{
            //    //获取Ip
            //    string ip = LocalIP.GetLocalIP();
            //    //获取系统的日志
            //    string msg = $"时间:{DateTime.Now},Ip:{ip},已完成控制器:{controller},页面:{action}\r\n";
            //    //获取文件路径
            //    string path = "D:\\Action.txt";
            //    File.AppendAllText(path, msg);
            //}
            //else
            //{
            //    //获取用户名
            //    string username = UserInfoContext.userInfoContext.UserInfo.UserName;
            //    //获取系统的日志
            //    string msg = $"时间:{DateTime.Now},用户名:{username},已完成控制器:{controller},页面:{action}\r\n";
            //    //获取文件路径
            //    string path = "D:\\Action.txt";
            //    File.AppendAllText(path, msg);
            //}
        }



        /// <summary>
        /// 调用控制器对应的Action方法之后页面渲染之后的操作
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {

        }

        /// <summary>
        /// 调用控制器对应的Action方法之后页面渲染之前的操作
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {

        }
    }
}